package Top_Interview_Questions.Temp;

/**
 * @Author: 吕庆龙
 * @Date: 2020/3/9 12:17
 * <p>
 * 官方题解结合
 * https://leetcode-cn.com/problems/reverse-integer/solution/hua-jie-suan-fa-7-zheng-shu-fan-zhuan-by-guanpengc/
 * 一起看
 */
public class _0007 {

    public static void main(String[] args) {
        _0007 test = new _0007();
        test.reverse(120);
    }

    public int reverse(int x) {
        int ans = 0;
        while (x != 0) {
            int pop = x % 10;
            if (ans > Integer.MAX_VALUE / 10 || (ans == Integer.MAX_VALUE / 10 && pop > 7))
                return 0;
            if (ans < Integer.MIN_VALUE / 10 || (ans == Integer.MIN_VALUE / 10 && pop < -8))
                return 0;
            ans = ans * 10 + pop;
            x /= 10;
        }
        return ans;
    }


}
